
/* Private define ------------------------------------------------------------*/
#include "var.h"
#include "TM52F1376_bsp.h"
#include "global.h"
#include "display.h"
#include "TimeEvent.h"
#include "math.h"
#include "string.h"
/*********************************************************************
 * MACROS
 */
/*********************************************************************
 * TYPEDEFS
 */

/*********************************************************************
 * CONSTANTS
 */

/*********************************************************************
 * GLOBAL VARIABLES
 */

/*********************************************************************
 * @fn     void ErrCodeStatisticHandle(void)
 *
 * @brief  故障代码统计
 *
 * @param   none
 *
 * @return  none
 */
void ErrCodeStatisticHandle(void)
{
	memset((uint8_t *)ErrCodeList, 0x00, sizeof(uint16_t) * ErrCodelistNum);
	e = 0;
	if (ErrorState.allBits != 0)
	{
		ErrPageMax = 0;
		// if (ErrorState.OneBit.Ntc0Error == 1)
		// {
		// 	ErrCodeList[e] = E0;
		// 	e++;
		// }
		if (cfgPara[CoolingMode].defrosteCycleTime != 0 && cfgPara[CoolingMode].defrosteType == 1) // 需要除霜
		{
			if (ErrorState.OneBit.Ntc1Error == 1)
			{
				ErrCodeList[e] = E1;
				e++;
			}
		}
		if (ErrorState.OneBit.AH == 1)
		{
			ErrCodeList[e] = EH;
			e++;
		}
		if (ErrorState.OneBit.AL == 1)
		{
			ErrCodeList[e] = EL;
			e++;
		}
		ErrPageMax = e;
	}
	// else
	// {
	// 	ErrPage = 0;
	// }
}
/*********************************************************************
 * @fn     void SymbolRefurbish(void)
 *
 * @brief  符号更新
 *
 * @param   none
 *
 * @return  none
 */
void SymbolRefurbish(void)
{
	timer_10msDB[SymbolRefurbish_TaskId].Flag = 0;
	timer_10msDB[SymbolRefurbish_TaskId].Enable = TRUE;
	timer_10msDB[SymbolRefurbish_TaskId].Time = 250; // 100ms

	Flag1.OneBit.KeyScan = 1;

	if (ErrorState.OneBit.Ntc0Error == 0)
	{
		updateCount++;
		if (updateCount > 39)
		{
			updateCount = 0;
			DisplayTempUpdate_Evt();
		}
	}

	if (Flag3.OneBit.TestMode == 0)
	{
		TM1650_SystemCmd(0x10, 0x00, 0x00, 0x01);

		// ErrCodeStatisticHandle();

		if (QuickFrozenState == quickFrozening)
		{
			disDotStatus.OneBit.QuickFrozening = ON;
		}
		else
		{
			disDotStatus.OneBit.QuickFrozening = OFF;
		}

		disDotStatus.OneBit.Lock = Flag2.OneBit.Lock;

		if (m_PageInfor.m_Type == 0)
		{
			if (CoolingMode == 0) // 冷冻
			{
				disDotStatus.OneBit.Colds = OFF;
				disDotStatus.OneBit.Freeze = ON;
			}
			else
			{
				disDotStatus.OneBit.Colds = ON;
				disDotStatus.OneBit.Freeze = OFF;
			}
		}
		else
		{
			if (DuplicateData == 0) // 冷冻
			{
				disDotStatus.OneBit.Colds = OFF;
				disDotStatus.OneBit.Freeze = ON;
			}
			else
			{
				disDotStatus.OneBit.Colds = ON;
				disDotStatus.OneBit.Freeze = OFF;
			}
		}

		if (Flag3.OneBit.factoryMode == 1) // 测试下速冻图标闪烁
		{
			if (Flag2.OneBit.FactoryModeSymolBlink == DISPLAY) // 温度和dF交替显示
			{
				disDotStatus.OneBit.Freeze = ON;
			}
			else
			{
				disDotStatus.OneBit.Freeze = OFF;
			}
			Flag2.OneBit.FactoryModeSymolBlink = ~Flag2.OneBit.FactoryModeSymolBlink;
		}
		DisplayDotSymbol(disDotStatus);
	}
}
/*********************************************************************
 * @fn      void DisplayRefurbish(void)
 *
 * @brief   ÊýÂë¹ÜÏÔÊ¾ÎÂ¶ÈÊý¾Ý
 *
 * @param   none
 *
 * @return  none
 */
void DisplayTempUpdate_Evt(void)
{
	/******变化逻辑******/
	DiffTemp = abs(TargetTemp - DisplayTemp);
	if (DiffTemp < 50)
	{
		offsetTemp = 1;
	}
	else if (DiffTemp >= 50 && DiffTemp <= 100)
	{
		offsetTemp = 3;
	}
	else if (DiffTemp > 100)
	{
		offsetTemp = 5;
	}

	if (DisplayTemp < TargetTemp)
	{
		DisplayTemp += offsetTemp;
	}
	else if (DisplayTemp > TargetTemp)
	{
		DisplayTemp -= offsetTemp;
	}
}
/*********************************************************************
 * @fn      void DisplayRefurbish(void)
 *
 * @brief   柜内传感器正常显示温度
 *
 * @param   none
 *
 * @return  none
 */
void NtcNormalTempDisplay(void)
{
	if (Flag3.OneBit.factoryMode == 1) // 进入工厂测试逻辑下
	{
		DisplayTempValue(DisplayTemp);
	}
	else
	{
		if (DefrostState == defrostingCycle) // 除霜周期内
		{
			if (Flag2.OneBit.PretendDisplay == 1) // 正常显示//除霜后一段时间假显
			{
				if (cfgPara[CoolingMode].defrosteShowMode == 1) // 除霜时显示进入除霜前的温度。
				{
					DisplayTemp = BeforeDefrosteTemp;
				}
				else
				{
					DisplayTemp = cfgPara[CoolingMode].St * 10;
				}
			}
			DisplayTempValue(DisplayTemp);
		}
		else // 除霜过程中
		{
			if (cfgPara[CoolingMode].defrosteShowMode == 0) // d6= 0时，在除霜时，传感器测量的温度和dF交替显示。
			{
				// DisplayTemp = TemperatureInside;
				/******变化逻辑******/
				// DisplayTempUpdate_Evt();
				if (Flag2.OneBit.Blink == DISPLAY) // 温度和dF交替显示
				{
					DisplayTempValue(DisplayTemp);
				}
				else
				{
					DisplayDatasSymbol(Code_d, Code_F);
				}
				Flag2.OneBit.Blink = ~Flag2.OneBit.Blink;
			}
			else if (cfgPara[CoolingMode].defrosteShowMode == 1) // 除霜时显示进入除霜前的温湿度。
			{
				DisplayTempValue(BeforeDefrosteTemp); // BeforeDefrosteTemp,DisplayTemp
			}
			else if (cfgPara[CoolingMode].defrosteShowMode == 2) // 除霜时显示dF
			{
				DisplayDatasSymbol(Code_d, Code_F);
			}
		}
	}
}
/*********************************************************************
 * @fn      void DisplayRefurbish(void)
 *
 * @brief   ÊýÂë¹ÜÏÔÊ¾ÎÂ¶ÈÊý¾Ý
 *
 * @param   none
 *
 * @return  none
 */
void DisplayRefurbish(void)
{
	timer_10msDB[DisplayRefurbish_TaskId].Flag = 0;
	timer_10msDB[DisplayRefurbish_TaskId].Enable = TRUE; // ENABLE;
	timer_10msDB[DisplayRefurbish_TaskId].Time = 2000;	 // 200U;//500ms

	Flag1.OneBit.KeyScan = 1;

	switch (m_PageInfor.m_Type)
	{
	case 0x00:
	{
		if (Flag3.OneBit.defrosteTempShow == FALSE) // 没有进行化霜温度查看
		{
			ErrCodeStatisticHandle();
			switch (displayState)
			{
			case 0: // 柜内温度探头异常状态
				if (ErrorState.OneBit.Ntc0Error == 0)
				{
					NtcNormalTempDisplay();
				}
				else
				{
					DisplayDatasSymbol(Code_E, Num_0);
				}
				displayState = 1;
				break;

			case 1: // 化霜温度探头异常状态
				if (ErrPageMax != 0 && (ErrorState.allBits & 0xFC) != 0)
				{
					if (ErrPage > (ErrPageMax - 1))
					{
						ErrPage = 0;
						displayState = 0;
						timer_10msDB[DisplayRefurbish_TaskId].Time = 10;
					}
					else
					{
						DisplayDatasSymbol(HI_UINT16(ErrCodeList[ErrPage]), LO_UINT16(ErrCodeList[ErrPage]));
						ErrPage++;
					}
				}
				else
				{
					ErrPage = 0;
					displayState = 0;
					timer_10msDB[DisplayRefurbish_TaskId].Time = 10;
				}
				break;
			default:
				break;
			}
		}
		else // 查看化霜温度探头
		{
			if (cfgPara[CoolingMode].defrosteCycleTime != 0 && cfgPara[CoolingMode].defrosteType == 1)
			{
				if (ErrorState.OneBit.Ntc1Error == 0) // 化霜传感器正常
					DisplayTempValue(DefrostTemp);
				else
					DisplayDatasSymbol(Code_E, Num_1);
			}
			else
			{
				displayCode[2] = Code_y;
				displayCode[1] = Code_y;
				displayCode[0] = Code_y;
				DisplayAllSymbol(displayCode);
			}
		}
	}
	break;

	case 0x01:
		DisplayCfgListCode(CfgList[m_PageInfor.m_CfgPg]);
		break;

	case 0x02:
		Timer_Event_Set(&timer_10msDB[DisplayRefurbish_TaskId], 500, ENABLE, 0, DisplayRefurbish); // 500ms
		if (Flag2.OneBit.Blink == 1)															   // 传感器故障数码管闪烁 E1
		{
			DisplayCfgData(duplicate_sptr[m_PageInfor.m_CfgPg], CfgParaRange[m_PageInfor.m_CfgPg].dp);
		}
		else
		{
			DisplayCfgListCode(0x0000);
		}
		Flag2.OneBit.Blink = ~Flag2.OneBit.Blink;
		break;

	default:
		break;
	}
}

/*********************************************************************
 * @fn      void DisplayTempValue( int16_t tempvalue)
 *
 * @brief   数码管显示温度数据
 *
 * @param   tempValue 温度值   放大10
 *
 * @return  none
 */

void DisplayTempValue(int16_t tempvalue)
{
	tempA = abs(tempvalue);
	if (tempvalue > 999 || tempvalue < -999)
	{
		displayCode[2] = Code_n;
		displayCode[1] = Code_u;
		displayCode[0] = Code_L;
		DisplayAllSymbol(displayCode);
		return;
	}

	hundreds = tempA / 100;
	decade = tempA % 100 / 10;
	unit = tempA % 100 % 10;

	if (tempvalue < -199)
	{
		displayCode[2] = Code_y;
		displayCode[1] = NumCode[hundreds];
		displayCode[0] = NumCode[decade];
	}
	else if (tempvalue >= -199 && tempvalue <= -100)
	{
		displayCode[2] = NumCode[1] | Code_y;
		displayCode[1] = NumCode[decade] | Code_dp;
		displayCode[0] = NumCode[unit];
	}
	else if (tempvalue > -100 && tempvalue < 0)
	{
		displayCode[2] = Code_y;
		displayCode[1] = NumCode[decade] | Code_dp;
		displayCode[0] = NumCode[unit];
	}
	else if (tempvalue >= 0 && tempvalue < 100)
	{
		displayCode[2] = 0x00;
		displayCode[1] = NumCode[decade] | Code_dp;
		displayCode[0] = NumCode[unit];
	}
	else if (tempvalue >= 100)
	{
		displayCode[2] = NumCode[hundreds];
		displayCode[1] = NumCode[decade] | Code_dp;
		displayCode[0] = NumCode[unit];
	}
	DisplayAllSymbol(displayCode);
}

/*********************************************************************
 * @fn      void DisplayCfgListCode(uint16_t cfgCode )
 *
 * @brief   数码管显示配置菜单栏
 *
 * @param   cfgCode 菜单栏编号
 *
 * @return  none
 */
void DisplayCfgListCode(uint16_t cfgCode)
{
	hi = HI_UINT16(cfgCode);
	lo = LO_UINT16(cfgCode);

	// displayCode[3] = 0x00;
	displayCode[2] = 0x00;
	displayCode[1] = hi;
	displayCode[0] = lo;

	DisplayAllSymbol(displayCode);
}

/*********************************************************************
 * @fn      void DisplayCfgData( int8_t cfgData )
 *
 * @brief   数码管显示配置相关数据
 *
 * @param   cfgData 配置数据
 *
 * @return  none
 */
void DisplayCfgData(int16_t cfgData, uint8_t dp)
{

	hundreds = abs(cfgData) / 100;
	decade = abs(cfgData) % 100 / 10;
	unit = abs(cfgData) % 10;

	if (cfgData < -199 || cfgData > 199)
	{
		displayCode[2] = Code_y;
		displayCode[1] = Code_y;
		displayCode[0] = Code_y;
		DisplayAllSymbol(displayCode);
		return;
	}
	if (cfgData >= -199 && cfgData <= -100)
	{
		displayCode[2] = NumCode[1] | Code_y;
	}
	else if (cfgData > -100 && cfgData < 0)
	{
		displayCode[2] = Code_y;
	}
	else if (cfgData >= 0 && cfgData < 100)
	{
		displayCode[2] = 0x00;
	}
	else if (cfgData >= 100 && cfgData <= 199)
	{
		displayCode[2] = NumCode[1];
	}

	if (dp == ENABLE) // 存在小数点
	{
		displayCode[1] = NumCode[decade] | Code_dp;
	}
	else
	{
		displayCode[1] = NumCode[decade];
	}
	displayCode[0] = NumCode[unit];

	DisplayAllSymbol(displayCode);
}

/*********************************************************************
 * @fn     void DisplayDatasSymbol(uint8_t data1, uint8_t data2 )
 *
 * @brief   数码管显示
 *
 * @param   data1 第一位数据 data2 第二位数据
 *
 * @return  none
 */
void DisplayDatasSymbol(uint8_t data1, uint8_t data2)
{
	// displayCode[3] = (displayCode[3] & 0xFD);
	displayCode[2] = 0x00;
	displayCode[1] = data1;
	displayCode[0] = data2;

	DisplayAllSymbol(displayCode);
}
/*********************************************************************
 * @fn     void //DisplayAllSymbol2(uint8_t data1, uint8_t data2 )
 *
 * @brief   数码管显示
 *
 * @param   data1 第一位数据 data2 第二位数据
 *
 * @return  none
 */
void DisplayAllSymbol(uint8_t *codes)
{
#ifdef tm1650
	TM1650_send(GID3, *codes);		 // DigitalShow1
	TM1650_send(GID2, *(codes + 1)); // DigitalShow2
	TM1650_send(GID1, *(codes + 2)); // DigitalShow3
	TM1650_send(GID4, *(codes + 3)); // DigitalShow3
#else
	LED_Write_Seg(0, *codes);
	LED_Write_Seg(1, *(codes + 1));
	LED_Write_Seg(2, *(codes + 2));
	LED_Write_Seg(3, *(codes + 3));
#endif
}

/*********************************************************************
 * @fn     void DisplayDotSymbol(displayDot_t dotStatus)
 *
 * @brief   数码管显示各种状态符号
 *
 * @param   displayDot_t dotStatus
 *
 * @return  none
 */
void DisplayDotSymbol(displayDot_t dotStatus)
{
	if (dotStatus.OneBit.Colds == 1)
		displayCode[3] = displayCode[3] | 0x02;
	else
		displayCode[3] = displayCode[3] & 0xFD;

	if (dotStatus.OneBit.Freeze == 1)
		displayCode[3] = displayCode[3] | 0x04;
	else
		displayCode[3] = displayCode[3] & 0xFB;

	if (dotStatus.OneBit.Lock == 1)
		displayCode[3] = displayCode[3] | 0x08;
	else
		displayCode[3] = displayCode[3] & 0xF7;

	if (dotStatus.OneBit.QuickFrozening == 1)
		displayCode[3] = displayCode[3] | 0x01;
	else
		displayCode[3] = displayCode[3] & 0xFE;

#ifdef tm1650
	TM1650_send(GID4, displayCode[3]); // DigitalShow3
#else
	LED_Write_Seg(3, displayCode[3]);
#endif
}
